import Taro, { Component } from '@tarojs/taro'
import { View, Text,Image } from '@tarojs/components'
import { Button }  from 'taro-ui'
import './index.less'
import newLogo from '../../img/newLogo.png'

export default class Login extends Component {

  config = {
    navigationBarTitleText: '授权登录'
  }


  onLoad() {
    // 查看是否授权
    Taro.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          Taro.getUserInfo({
            success() {
              console.log(res.userInfo)
            },
            fail(){
             console.log(res.userInfo)
            }
          });
        }
      }
    })
  }

  onGetUserInfo(e) {
    console.log('authorization e',e.detail.userInfo)
    if (e.detail.userInfo){
      Taro.switchTab({
        url:'../home/index'
      })
   } else {
     //用户按了拒绝按钮
     Taro.showToast({
       title:'您取消了授权！',
       icon:'none'
     })
   }
  }

  componentDidMount () { }

  componentWillUnmount () { }

  componentDidShow () { }

  componentDidHide () { }

  render () {
    return (
      <View className='Login'>
        <Image src={newLogo} className='newLogo' />
        <View className='line' />
        <View className='text'>
          <Text className='apply'>申请获取以下权限</Text>
          <Text className='information'>获得你的公开信息（昵称、头像等）</Text>
        </View>
        <Button
          className='bottom'
          openType='getUserInfo'
          onGetUserInfo={this.onGetUserInfo}
        >
          <Text className='dl'>授权登录</Text>
        </Button>
      </View>
    )
  }
}
